﻿using BeerStats.Data;
using BeerStats.WebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace BeerStats.WebAPI.Controllers
{
    public class BeerTypesController : BaseApiController
    {
        [HttpGet]
        [ActionName("all")]
        public IQueryable<BeerTypesModel> GetAll()
        {
            return this.ExecuteOperationAndHandleExceptions(() =>
            {
                var context = new BeerStatsDbContext();
                var types = (from type in context.BeerTypes
                             select new BeerTypesModel()
                             {
                                 Id = type.Id,
                                 Name = type.Name,
                                 Description = type.Description,
                                 Beers = (from beer in type.Beers
                                         select new BeerFullModel() 
                                         {
                                             BeerId = beer.BeerId,
                                             BeerName = beer.Name,
                                             ImageSource = beer.ImageSource,
                                             LabelSource = beer.LabelSource,
                                             AlcoholByVolume = beer.AlcoholByVolume,
                                             BreweryName = beer.BreweryName,
                                             Description = beer.Description,
                                             Rating = beer.Rating
                                         })
                             }).AsQueryable();

                return types;

            });
        }
    }
}
